/**
 * 
 */
package com.jie.eclipse.pan.views;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.ui.palette.DefaultPaletteViewerPreferences;
import org.eclipse.gef.ui.palette.PaletteViewer;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.dnd.HTMLTransfer;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.part.ViewPart;

import com.jie.eclipse.pan.Activator;
import com.jie.eclipse.pan.dao.QMDao;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.gef.action.OpenBrowserAction;
import com.jie.eclipse.pan.gef.action.RefreshAction;
import com.jie.eclipse.pan.gef.listener.PanHelperSelectionChangedListener;
import com.jie.eclipse.pan.gef.listener.PanHelperTransferDragSourceListener;
import com.jie.eclipse.pan.utils.gef.PaletteFactory;

/**
 * @author yanjie
 * 
 */
public class PanDeveloperhelperView extends ViewPart {
	protected PaletteViewer paletteViewer = null;
	private PaletteEntry selectEntry = null;
	
	private Action refresh;
	
	@Override
	public void createPartControl(Composite parent) {
		QMDao.getInstance().setStore(Activator.getDefault().getPreferenceStore());
		
		parent.setLayout(new FillLayout());
		this.paletteViewer = new PaletteViewer();
		this.paletteViewer.enableVerticalScrollbar(true);
		this.paletteViewer.setPaletteViewerPreferences(new DefaultPaletteViewerPreferences(
						Activator.getDefault().getPreferenceStore()));
		configurePaletteViewer(this.paletteViewer);
		PaletteRoot root = PaletteFactory.getInstance().createPalette();
		this.paletteViewer.setPaletteRoot(root);
		this.paletteViewer.addDragSourceListener(new PanHelperTransferDragSourceListener(this,TextTransfer.getInstance()));
		this.paletteViewer.addDragSourceListener(new PanHelperTransferDragSourceListener(this,HTMLTransfer.getInstance()));
		this.paletteViewer.addSelectionChangedListener(new PanHelperSelectionChangedListener(this));
		
		this.paletteViewer.createControl(parent);
		
		
		makeActions();
		//完成工具栏
		contributeToActionBars();
		parent.layout();
	}

	@Override
	public void setFocus() {
			
	}

	protected void configurePaletteViewer(PaletteViewer paramPaletteViewer) {
		paramPaletteViewer.setContextMenu(new PanPaletteContextMenuProvider(this, paramPaletteViewer));
	}
	
	/**
	 * 
	 */
	public PaletteEntry getEntryFromSelection(ISelection selection) {
		if(selection == null || selection.isEmpty() ) return null;
		if(selection instanceof IStructuredSelection){
			IStructuredSelection structuredSelection = (IStructuredSelection)selection;
			Object selectObj = structuredSelection.getFirstElement();
			if(selectObj instanceof PaletteEntry){
				return (PaletteEntry)selectObj;
			}else if(selectObj instanceof EditPart && ((EditPart)selectObj).getModel() instanceof PaletteEntry){
				return (PaletteEntry)((EditPart)selectObj).getModel();
			}
		}
		return null;
	}
	
	public PaletteEntry getSelectEntry() {
		return selectEntry;
	}

	public void setSelectEntry(PaletteEntry selectEntry) {
		this.selectEntry = selectEntry;
	}
	
	public PaletteViewer getViewer() {
		return paletteViewer;
	}
	
	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}
	
	
	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(refresh);
		BaseDataEntity config = new BaseDataEntity();
		config.spsv("id", "ctn027");
		config.spsv("text", "实体维护");
		config.spsv("url", "http://localhost/jmPro/admin/page/page027.jsp?"+QMDao.getInstance().getLoginStr());
		manager.add(new OpenBrowserAction(this,config));
		
		config = new BaseDataEntity();
		config.spsv("id", "ctn028");
		config.spsv("text", "实体栏位维护");
		config.spsv("url", "http://localhost/jmPro/admin/page/page028.jsp?"+QMDao.getInstance().getLoginStr());
		manager.add(new OpenBrowserAction(this,config));
		
		config = new BaseDataEntity();
		config.spsv("id", "page026");
		config.spsv("text", "組件類型設定");
		config.spsv("url", "http://localhost/jmPro/admin/page/page026.jsp?"+QMDao.getInstance().getLoginStr());
		manager.add(new OpenBrowserAction(this,config));
		
		
		config = new BaseDataEntity();
		config.spsv("id", "page025");
		config.spsv("text", "組件設定");
		config.spsv("url", "http://localhost/jmPro/admin/page/page025.jsp?"+QMDao.getInstance().getLoginStr());
		manager.add(new OpenBrowserAction(this,config));
		
		config = new BaseDataEntity();
		config.spsv("id", "home");
		config.spsv("text", "Pan 幫助系統");
		config.spsv("url", "http://localhost/jmPro/admin/home.jsp?"+QMDao.getInstance().getLoginStr());
		manager.add(new OpenBrowserAction(this,config));
		
		manager.add(new Separator());
	}
	

	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(refresh);
		manager.add(new Separator());
	}
	
	//创建菜单和工具栏对应的Action
	private void makeActions() {
		refresh = new RefreshAction(this);
	}

}
